﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BOMS.DAL;
using System.Data;

namespace BOMS.WebUI
{
    public partial class Menu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
            tvOrg.TreeNodePopulate += new TreeNodeEventHandler(tvOrg_TreeNodePopulate);
           
            //tvOrg.PopulateNodesFromClient = true;
            if (!IsPostBack)
            {             

                string userId = HttpContext.Current.User.Identity.Name;
                showMenu(userId);
            }
        }

        protected void tvOrg_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            BLL.M_SubLans bll = new BLL.M_SubLans();
            string PID = e.Node.Value;
            DataTable batteryRoomTbl = bll.GetOrgByParentID(PID);
             for (int i = 0; i < batteryRoomTbl.Rows.Count; i++)
             {
                 TreeNode BatteryRoom = new TreeNode();
                 BatteryRoom.Text = batteryRoomTbl.Rows[i]["ShortName"].ToString();
                 BatteryRoom.Value = batteryRoomTbl.Rows[i]["OrgID"].ToString();
                 BatteryRoom.PopulateOnDemand = false;
                 BatteryRoom.SelectAction = TreeNodeSelectAction.Select;
                // BatteryRoom.NavigateUrl = string.Format("javascript:alert('{0}');",BatteryRoom.Value);
                // BatteryRoom.NavigateUrl = "javascript:top.myTab.Cts('" + winceName + "','Web/RealStatus/groupDetail.aspx?winceIp=" + winceIp + "&winceMac=" + winceMac + "&lanName=" + HttpUtility.UrlEncodeUnicode(LanName) + "&winceName=" + HttpUtility.UrlEncodeUnicode(winceName) + "',true)";
                 BatteryRoom.NavigateUrl = "javascript:top.myTab.Cts('" + BatteryRoom.Value + "','Web/RealStatus/groupDetail.aspx?winceIp=" + BatteryRoom.Value + "&winceMac=" + BatteryRoom.Value + "&lanName=" + HttpUtility.UrlEncodeUnicode(BatteryRoom.Value) + "&winceName=" + HttpUtility.UrlEncodeUnicode(BatteryRoom.Value) + "',true)";

                 BatteryRoom.ImageUrl = "~/Images/zj.gif";
                 e.Node.ChildNodes.Add(BatteryRoom);
             }
        }
        protected void BtnRefresh_RefreshWinceTree(object sender, EventArgs e)
        {
            //bindWince();
        }
        /*
        private void bindWince(){
            this.tvWince.Nodes.Clear();//先清空根下面的wince子节点
            TreeNode root = new TreeNode("现场主机");          
            DataTable dtLan = SysAdmin.getParameter();
            string LanName = dtLan.Rows[0]["LanName"].ToString();
            root.Expanded = true;
            root.Text = LanName;
            root.Value = LanName;
            root.NavigateUrl = "javascript:top.myTab.Cts('" + LanName + "','Web/RealStatus/groupPage.aspx?lanname=" + HttpUtility.UrlEncodeUnicode(LanName) + "',true)";
            this.tvWince.Nodes.Add(root);


            DataTable dt = SysAdmin.getWinceOfLan();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
TreeNode node=new TreeNode();
                string winceName=dt.Rows[i]["Hostname"].ToString();
                string winceIp=dt.Rows[i]["ip"].ToString();
                string winceMac = dt.Rows[i]["mac"].ToString();
                node.Text=winceName;
                node.Value = winceMac;
                node.NavigateUrl = "javascript:top.myTab.Cts('" + winceName + "','Web/RealStatus/groupDetail.aspx?winceIp=" + winceIp + "&winceMac=" + winceMac + "&lanName=" + HttpUtility.UrlEncodeUnicode(LanName) + "&winceName=" + HttpUtility.UrlEncodeUnicode(winceName) + "',true)";
root.ChildNodes.Add(node);     
            }
        }*/
        protected void bindOrgnization()
        {
            BLL.M_SubLans bll = new BLL.M_SubLans();
            DataTable rootTbl = bll.GetOrgByParentID("");
            if(rootTbl.Rows.Count>0)
            {
                string RootName = rootTbl.Rows[0]["ShortName"].ToString();
                string RootID = rootTbl.Rows[0]["OrgID"].ToString();                
                TreeNode Root = new TreeNode(RootName, RootID);
                Root.PopulateOnDemand = false;

                tvOrg.Nodes.Add(Root);
                DataTable partCorpTbl = bll.GetOrgByParentID(RootID);
                for (int i = 0; i < partCorpTbl.Rows.Count; i++)
                {
                    TreeNode PartCorp = new TreeNode(partCorpTbl.Rows[i]["ShortName"].ToString(), partCorpTbl.Rows[i]["OrgID"].ToString());
                    PartCorp.PopulateOnDemand = true;
                    PartCorp.SelectAction = TreeNodeSelectAction.Expand;
                    
                    Root.ChildNodes.Add(PartCorp);
                }
            }
        }

        private void showMenu(string userId)
        {
            string moduleList = SysAdmin.getModuleListByUserId(userId);
            DataTable dt = SysAdmin.getAllModules();
            string[] moduleArr = moduleList.Split(',');

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string id = dt.Rows[i]["id"].ToString();
                string name = dt.Rows[i]["name"].ToString();
                if (Array.Exists(moduleArr, element => element.Equals(id)))
                {
                    switch (name)
                    {
                        case "现场主机":
                           // bindWince();
                           // this.tvWince.Visible = true;
                            bindOrgnization();                           
                            break;
                        case "停发电调度":
                            this.tvPowerControl.Visible = true;
                            break;
                        case "电池组维护":
                            this.tvBatgrpPM.Visible = true;
                            break;
                        case "报警中心":
                            this.tvWarning.Visible = true;
                            break;
                        case "数据分析":
                            this.tvData.Visible = true;
                            break;
                        case "系统设置":
                            this.tvSystem.Visible = true;
                            break;
                        //case "个人中心":
                        //    this.tvUser.Visible = true;
                        //    break;
                    }
                }
            }
        }
    }
}